#ifndef DSST_TRACKER_HPP
#define DSST_TRACKER_HPP

#include <opencv2/opencv.hpp>
#include <dlib/opencv.h>
#include <dlib/image_processing.h>
#include "base_tracker.hpp"

class DsstTracker : public BaseTracker
{
public:
    DsstTracker() {}
    ~DsstTracker() {}
    
    virtual int init(cv::Mat &img, cv::Rect2d &rect)
    {
        dlib::rectangle dlibRect;
        ToDlibRect(rect, dlibRect);
        dlib::cv_image<dlib::bgr_pixel> dlibImg(img);
        mTracker.start_track(dlibImg, dlibRect);
        mOriginBgrImg = img.clone();
        mOriginRect2d = rect;
        return 0;
    }

    virtual bool update(cv::Mat &img, cv::Rect2d &rect)
    {
        dlib::cv_image<dlib::bgr_pixel> dlibImg(img);
        mTracker.update(dlibImg);
        dlib::rectangle dlibRect = mTracker.get_position();
        ToCvRect(dlibRect, rect);
        return true;
    }

private:
    dlib::correlation_tracker mTracker;

private:
    void ToCvRect(dlib::rectangle &dr, cv::Rect2d &cr)
    {
        cr.x = dr.left();
        cr.y = dr.top();
        cr.width = dr.width();
        cr.height = dr.height();
    }

    void ToDlibRect(cv::Rect2d &cr, dlib::rectangle &dr)
    {
        dr.set_left(cr.x);
        dr.set_top(cr.y);
        dr.set_right(cr.x + cr.width);
        dr.set_bottom(cr.y + cr.height);
    }
};

#endif